package org.edu.modules.stu.common.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.stu.base.entity.VeBaseStudent;
import org.edu.modules.stu.base.service.VeBaseStudentService;
import org.edu.modules.stu.common.entity.VeBaseAppUser;
import org.edu.modules.stu.common.entity.VeBaseBanji;
import org.edu.modules.stu.common.entity.VeBaseTeacher;
import org.edu.modules.stu.common.service.VeBaseAppUserService;
import org.edu.modules.stu.common.service.VeBaseBanjiService;
import org.edu.modules.stu.common.service.VeBaseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = {"教师信息表"})
@RestController
@RequestMapping("/stu/veBaseTeacher")
@Slf4j
public class VeBaseTeacherController {

    @Autowired
    private VeBaseAppUserService veBaseAppUserService;

    @Autowired
    private VeBaseTeacherService veBaseTeacherService;

    @Autowired
    private VeBaseBanjiService veBaseBanjiService;

    @Autowired
    private VeBaseStudentService veBaseStudentService;

    /**
     * 查询班主任下的所有班级
     */
    @ApiOperation("查询班主任下的所有班级")
    @GetMapping("/teacherAllbanjin")
    public Result<?> teacherAllbanjin() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(sysUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(sysUser.getId());
        //根据工号查教师信息
        VeBaseTeacher baseTeacher = veBaseTeacherService.getByUserId(appUser.getUserId());
        //查班级
        List<VeBaseBanji> list = veBaseBanjiService.listByTeacherId(baseTeacher.getUserId());
        return Result.OK(list);
    }


    /**
     * 查询班主任下的所有学生
     */
    @ApiOperation("查询班主任下的所有学生")
    @GetMapping("/teacherAllStudent")
    public Result<?> teacherAllStudent() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(sysUser)) {
            return Result.error("请登录系统！");
        }
        List<VeBaseStudent> veBaseStudentList = veBaseTeacherService.getBySysUserId(sysUser.getId());
        return Result.OK(veBaseStudentList);
        /*//根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(sysUser.getId());
        //根据工号查教师信息
        VeBaseTeacher baseTeacher = veBaseTeacherService.getByUserId(appUser.getUserId());
        //查班级
        List<VeBaseBanji> baseBanjis = veBaseBanjiService.listByTeacherId(baseTeacher.getUserId());
        if (baseBanjis != null && baseBanjis.size() > 0) {
            //该教师是班主任，才查学生
            StringBuilder sb = new StringBuilder();
            for (VeBaseBanji baseBanji : baseBanjis) {
                //遍历班级，获取班级id
                sb.append(baseBanji.getId()).append(",");  // 添加分隔符
            }
            // 去除末尾多余的分隔符
            String baseBanjiIds = sb.length() > 0 ? sb.deleteCharAt(sb.length() - 1).toString() : "";
            String[] ids = baseBanjiIds.split(",");
            List<VeBaseStudent> list = veBaseStudentService.listByBanjinIds(ids);
            return Result.OK(list);
        } else {
            return Result.OK("该教师不是班主任");
        }*/
    }
}
